package view;

import mock.MockGameState;
import model.Task;
import util.ColorHelper;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Student
 * Date: 30/11/12
 * Time: 09:55
 * To change this template use File | Settings | File Templates.
 */
public class ColumnPanel extends JPanel {


    public ColumnPanel(String name, Color color, List<Task> tasks, int max) {
        setLayout(new GridLayout(max + 1, 1, 2, 2));
        TitledBorder border = BorderFactory.createTitledBorder(name);
        border.setTitleColor(color);
        setBorder(border);
        int weight = 0;
        for (final Task t : tasks) {
            weight += t.unitsOfWorkEstimated();
            JButton taskButton = new JButton(t.toString());
            taskButton.setBackground(color);
            taskButton.setForeground(ColorHelper.getOppositeColor(color));
            add(taskButton);
            taskButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    GamePanel instance = GamePanel.getInstance();
                    MockGameState mockGameState = MockGameState.getInstance();
                    mockGameState.moveTask(t);
                    if (instance != null) {
                        instance.update(mockGameState);
                    }

                }
            });
        }
        int size = tasks.size();
        int emptyBoxesNumber = max - size;
        for (int i = 0; i < emptyBoxesNumber; i++) {
            add(new JLabel());
        }
        JLabel totalLabel = new JLabel("Total: " + weight);
        totalLabel.setForeground(Color.white);
        setBackground(Color.white);
        add(totalLabel);
    }
}
